<template>
  <div class="manage">
    <el-dialog
      title="新增供应商"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- 表单信息 -->
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="供应商名称" prop="splName">
          <el-input
            placeholder="请输入供应商名称"
            v-model="form.splName"
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商电话" prop="splPhone">
          <el-input
            placeholder="请输入供应商电话"
            v-model="form.splPhone"
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商简介" prop="splContent">
          <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入供应商简介"
            v-model="form.splContent"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button class="addBtn" type="primary" @click="handlerAdd">
        + Add
      </el-button>
      <el-form :model="searchForm">
        <el-form-item class="search">
          <el-input
            placeholder="请输入名称"
            v-model="searchForm.name"
          ></el-input>
          <el-button type="primary" @click="searchName">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <el-table height="90%" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="splName" label="供应商名称"> </el-table-column>
        <el-table-column prop="splPhone" label="供应商电话"> </el-table-column>
        <el-table-column :min-width="400" prop="splContent" label="供应商简介">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row.sid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pageData.total"
        @current-change="handlePage"
        :page-size="onePageSize"
        :current-page.sync="pageData.curPage"
        class="pager"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getSupplier,
  addSupplier,
  editSupplier,
  delSupplier,
} from "@/api/drugInfo/supplier";

export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        sid: "",
        splName: "",
        splContent: "",
        splPhone: "",
      },
      formNull: {
        sid: "",
        splName: "",
        splContent: "",
        splPhone: "",
      },
      rules: {
        splName: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        splPhone: [
          { required: true, message: "请输入供应商电话", trigger: "blur" },
        ],
      },
      tableData: [
        {
          splName: "",
          splContent: "",
          splPhone: "",
        },
      ],
      modelType: 0, //0表示新增 1表示编辑
      onePageSize: 5,
      pageData: {
        total: 0, //当前总条数
        curPage: 1,
      },
      searchForm: {
        name: "",
      },
    };
  },
  methods: {
    //提交用户表单
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.modelType == 0) {
            //新增用户
            addSupplier(this.form).then(() => {
              //发送后的操作
              this.getList();
            });
          } else {
            editSupplier(this.form).then(() => {
              //更新后的操作
              this.getList();
            });
          }
          //修改页码为第一页
          this.pageData.curPage = 1;
          //关闭弹窗
          this.dialogVisible = false;
          this.$refs.form.resetFields();
        }
      });
    },
    handleClose() {
      // this.$refs.form.resetFields();
      this.form = this.formNull;
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.modelType = 1;
      this.dialogVisible = true;
      //深拷贝
    },
    handleDel(sid) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delSupplier(sid).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
            //修改页码为第一页
            this.pageData.curPage = 1;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getList(curPage, limit, sname = null) {
      if (curPage === null || limit == null) {
        curPage = 0;
        limit = this.onePageSize;
      }
      getSupplier({ curPage: curPage, limit: limit, sname: sname }).then(
        ({ data }) => {
          //获取数据
          const page = data.data;
          this.tableData = page.records;
          this.pageData.total = page.total;
        }
      );
    },
    searchName() {
      this.getList(null, null, this.searchForm.name);
    },
    handlerAdd() {
      this.modelType = 0;
      this.dialogVisible = true;
    },
    //选择页面的回调
    handlePage(val) {
      this.getList(val, this.onePageSize);
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 100%;
  .manage-header {
    display: flex;
    justify-content: space-between;
    .addBtn {
      height: 70%;
    }
  }
  .common-table {
    position: relative;
    height: calc(85%);
    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
}
</style>
